package com.labichaoka.chaoka.ui.bank.display;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.labichaoka.chaoka.BuildConfig;
import com.labichaoka.chaoka.R;
import com.labichaoka.chaoka.base.BaseActivity;
import com.labichaoka.chaoka.base.GlobalParams;
import com.labichaoka.chaoka.entity.BankCardListResponse;
import com.labichaoka.chaoka.entity.IsOpenAcountRequest;
import com.labichaoka.chaoka.entity.IsOpenAcountResponse;
import com.labichaoka.chaoka.ui.bank.bind.BankBindActivity;
import com.labichaoka.chaoka.utils.ToastUtils;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/*
* 银行卡列表
*
* */

public class BankCardDisplayActivity extends BaseActivity implements BankCardDisplayView{

    @BindView(R.id.bank_name)
    TextView bankName;
    @BindView(R.id.bank_num)
    TextView bankNum;
    @BindView(R.id.bank_logo)
    ImageView bankLogo;
    @BindView(R.id.change_card)
    ImageView changeCard;
    @BindView(R.id.bank_layout)
    LinearLayout bankLayout;
    @BindView(R.id.empty_layout)
    RelativeLayout emptyLayout;


    private BankCardDisplayPresenter presenter;

    @Override
    protected int initLayoutId() {
        return R.layout.activity_bank_card_display;
    }

    @Override
    protected void initView() {

    }

    @Override
    protected void initData() {
        presenter=new BankCardDisplayPresenterImpl(new BankCardDisplayInteractorImpl(),this);
    }

    @Override
    protected void onResume() {
        super.onResume();
        IsOpenAcountRequest request = new IsOpenAcountRequest();
        request.setCallBackUrl(BuildConfig.WEB_HOST + "blankPage");
        presenter.isOpenAcount(request);
    }
    @OnClick({R.id.change_card})
    public void click(View v){
        Bundle bundle=new Bundle();
        bundle.putInt("type",GlobalParams.CHANGE_BANK_CARD);
        gotoActivity(mContext, BankBindActivity.class, bundle);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public void showProgress() {
        showLoadingProgress();
    }

    @Override
    public void hideProgress() {
        hideLoadingProgress();
    }

    @Override
    public void showMessage(String str) {
        ToastUtils.show(str);
    }

    @Override
    public void queryBankList(BankCardListResponse response) {
        setUI(response);

    }

    @Override
    public void isOpenAcountCallback(IsOpenAcountResponse response) {
        presenter.getBankList();
    }

    private void setUI(BankCardListResponse response) {
        if (response.getData() == null) {
            bankLayout.setVisibility(View.GONE);
            emptyLayout.setVisibility(View.VISIBLE);
            return;
        }
        String completeStatus=response.getData().getCompleteStatus();
        if (!TextUtils.isEmpty(completeStatus)){
            if (completeStatus.equals("1")){
                changeCard.setVisibility(View.VISIBLE);
            }else{
                changeCard.setVisibility(View.GONE);
            }
        }

        String name=response.getData().getBelongedBankcard();
        String num=response.getData().getCardNo();
        bankName.setText(name);
        if (!TextUtils.isEmpty(num)){
            num=num.substring(num.length()-4,num.length());
        }
        bankNum.setText(num);

        Glide.with(mContext).load(response.getData().getLogoUrl()).into(bankLogo);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        presenter.onDestroy();
    }
}
